<?php

namespace App\Http\Controllers\CallingCards\Wechat;

use App\Http\Controllers\BaseController;
use App\Models\FeishuCallingCard\FeishuCallingCardUser;
use App\Models\FeishuCallingCard\FeishuCallingCardUserSource;
use Illuminate\Http\Request;

class SourceController extends BaseController
{
    public function record(Request $request)
    {
        $this->validate($request, [
            'shareType'   => 'required|int',
            'wechatScene' => 'required|string',
            'urlId'       => 'required|int',
        ]);
        $source = new FeishuCallingCardUserSource;
        $source->userId = auth('callingCardsWeChat')->user()->id;
        $source->shareType = (int)$request->shareType;
        $source->wechatScene = (string)$request->wechatScene;
        $source->invitorId = (string)$request->invitorId;
        $source->shopId = (int)$request->shopId;
        $source->urlId = (int)$request->urlId;
        $source->save();
        if (strlen($request->invitorId)) {
            auth('callingCardsWeChat')->user()->invitorId = $request->invitorId;
        }
        if (203 == $request->invitorId) {
            if ($request->shopId > 0) {
                auth('callingCardsWeChat')->user()->shopId = (int)$request->shopId;
            };
        } else if ($request->invitorId > 1) {
            auth('callingCardsWeChat')->user()->shopId = 0;
        }
        auth('callingCardsWeChat')->user()->save();
        return $this->success();
    }
}
